package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;

public class Alerty extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public Alerty() {
		super("Alerty");
	}

	private JTable table;
	private JButton dodaj, zamknij;
	private JPanel dolnyPanel;

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand() == "Zamknij") {
			this.dispose();
		} else if (e.getActionCommand() == "Dodaj") {
			JFrame dPF = new DodajPracownikaInputFrame();
			((DodajPracownikaInputFrame) dPF).start();
		}
	}

	public void start() {

		table = new JTable((new AlertRetriever()).get("Alerty"));
		for (int i = 0; i < table.getColumnCount(); i++) {
			if (i == 11)
				table.getColumnModel().getColumn(i)
						.setCellEditor(new CheckBoxCellEditor());
			else
				table.getColumnModel().getColumn(i)
						.setCellRenderer(new CustomRenderer());
		}

		TableColumn column = null;
		for (int i = 0; i < 11; i++) {
			column = table.getColumnModel().getColumn(i);
			switch (i) {
			case 0:
				column.setPreferredWidth(20);
				break;
			case 1:
				column.setPreferredWidth(50);
				break;
			case 2:
				column.setPreferredWidth(70);
				break;
			case 3:
				column.setPreferredWidth(300);
				break;
			case 4:
				column.setPreferredWidth(60);
				break;
			case 5:
				column.setPreferredWidth(50);
				break;
			case 6:
				column.setPreferredWidth(120);
				break;
			case 7:
				column.setPreferredWidth(40);
				break;
			case 8:
				column.setPreferredWidth(50);
				break;
			case 9:
				column.setPreferredWidth(100);
				break;
			case 10:
				column.setPreferredWidth(50);
				break;
			case 11:
				column.setPreferredWidth(40);
				break;
			}
		}
		table.getColumnModel().getColumn(7)
				.setCellEditor(new CheckBoxCellEditor());
		dolnyPanel = new JPanel();
		dodaj = new JButton("Dodaj");
		zamknij = new JButton("Zamknij");
		dodaj.addActionListener(this);
		zamknij.addActionListener(this);

		setLayout(new BorderLayout());
		add(table.getTableHeader(), BorderLayout.PAGE_START);
		add(new JScrollPane(table), BorderLayout.CENTER);
		add(dolnyPanel, BorderLayout.SOUTH);
		dolnyPanel.add(dodaj);
		dolnyPanel.add(zamknij);

		setSize(1200, 600);
		setVisible(true);
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		setLocation((screenSize.width - 1200) / 2,
				(screenSize.height - 600) / 2);
		setDefaultCloseOperation(HIDE_ON_CLOSE);
	}

	private class CheckBoxCellEditor extends AbstractCellEditor implements
			TableCellEditor {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		protected JCheckBox checkBox;

		public CheckBoxCellEditor() {
			checkBox = new JCheckBox();
			checkBox.setHorizontalAlignment(SwingConstants.CENTER);
			checkBox.setBackground(Color.white);
			
		}

		public Component getTableCellEditorComponent(JTable table,
				Object value, boolean isSelected, int row, int column) {
			if (((Integer) value) == 1) {
				checkBox.setSelected(true);
			} else
				checkBox.setSelected(false);
			return checkBox;
		}

		public Object getCellEditorValue() {
			return Boolean.valueOf(checkBox.isSelected());
		}
	}

	public class AlertRetriever

	{
		public DefaultTableModel get(String table) {

			Vector<Object> columnNames = new Vector<Object>();
			Vector<Object> data = new Vector<Object>();

			try {
				Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
				Connection conn = DriverManager.getConnection(
						"jdbc:sqlserver://localhost:1433;databaseName=ABSI",
						"Admin", "Admin");
				Statement statement = conn.createStatement();
				String queryString = "SELECT * FROM [ABSI].[dbo].[" + table
						+ "]";
				ResultSet rs = statement.executeQuery(queryString);
				ResultSetMetaData md = rs.getMetaData();
				int columns = md.getColumnCount();
				for (int i = 1; i <= columns; i++) {
					columnNames.addElement(md.getColumnName(i));

				}

				while (rs.next()) {
					Vector<Object> row = new Vector<Object>(columns);

					for (int i = 1; i <= columns; i++) {
						row.addElement((Object) rs.getObject(i));
					}

					data.addElement(row);
				}

				rs.close();
				statement.close();
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return new DefaultTableModel(data, columnNames) {

				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				@Override
				public Class<?> getColumnClass(int columnIndex) {
					switch (columnIndex) {
					case 0:
						return String.class;

					case 1:
						return String.class;

					case 2:
						return String.class;

					case 3:
						return String.class;

					case 4:
						return String.class;

					case 5:
						return String.class;

					case 6:
						return String.class;

					case 7:
						return String.class;

					case 8:
						return String.class;

					case 9:
						return String.class;
					case 10:
						return String.class;

					case 11:
						return Boolean.class;

					}
					return null;
				}
			};

		}
	}

}

class CustomRenderer extends DefaultTableCellRenderer {
	private static final long serialVersionUID = 6703872492730589499L;

	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {

		Component comp = super.getTableCellRendererComponent(table, value,
				isSelected, hasFocus, row, column);
		String valueCheck1 = (String) table.getValueAt(row, 4);
		String valueCheck2 = (String) table.getValueAt(row, 5);
		if ((valueCheck1.equals("Bardzo wysoki"))) {
			if ((valueCheck2.equals("Otwarty"))) {
				setOpaque(true);
				comp.setBackground(Color.red);
			} else {
				setOpaque(true);
				comp.setBackground(new Color(255, 153, 153));
			}
		} else if ((valueCheck1.equals("Wysoki"))) {
			if ((valueCheck2.equals("Otwarty"))) {
				setOpaque(true);
				comp.setBackground(Color.yellow);
			} else {
				setOpaque(true);
				comp.setBackground(new Color(255, 255, 153));
			}
		} else if ((valueCheck1.equals("Normalny"))) {
			if ((valueCheck2.equals("Otwarty"))) {
				setOpaque(true);
				comp.setBackground(Color.GREEN);
			} else {
				setOpaque(true);
				comp.setBackground(new Color(204, 255, 153));
			}
		} else
			setOpaque(false);

		return (comp);

	}

}
